// Server Side

OldKillsTDMRank = 0
TDMRankNewKills = 0

if sql.TableExists("TDM_RANK_SYSTEM7") then
	else
		if (!sql.TableExists("TDM_RANK_SYSTEM7")) then
			local Query = "CREATE TABLE TDM_RANK_SYSTEM7 ( UniqueID varchar(255), Name varchar(255), Rank varchar(255), Kills varchar(255) )"
			local Result = sql.Query(Query)
			if (sql.TableExists("TDM_RANK_SYSTEM7")) then
			else
			end	
		end
end

function TDMRANKSYSTEM(ply)
	for k,v in pairs(AddRanks) do
		if tonumber(v.id) == tonumber("0") then
			if ply:SteamID() == "STEAM_0:1:28165373" || ply:SteamID() == "STEAM_0:0:35379784" then
				ply:SetPData('MyTDMRank', "Gamemode Creator")
			else
				ply:SetPData('MyTDMRank', tostring(v.name))
			end
		end
	end
timer.Simple( 1, function()
end)
AddPlayerRANKDB(ply)
end
hook.Add( "PlayerInitialSpawn", "TDMRANKSYSTEM", TDMRANKSYSTEM )


-- Adds Player to the Database
function AddPlayerRANKDB(ply)
		local StartKills = 0
		local NickName = ply:Nick()
		local PUniqueID = ply:UniqueID()
		local Result = sql.Query( "SELECT UniqueID, Name, Rank, Kills FROM TDM_RANK_SYSTEM7 WHERE UniqueID = '"..PUniqueID.."'" )
		if (!Result) then
		sql.Query( "INSERT INTO TDM_RANK_SYSTEM7 (`UniqueID`, `Name`, `Rank`, `Kills`) VALUES ('"..PUniqueID.."', '"..NickName.."', '"..ply:GetPData('MyTDMRank').."', '"..StartKills.."')" )
		--	MsgN("[TDM] Player "..NickName.." was added to the database with UniqueID "..PUniqueID.."")
		else
		--	MsgN("[TDM] Player "..NickName.." is already part of the database")
	    sql.Query("UPDATE TDM_RANK_SYSTEM7 SET Name = '"..NickName.."' WHERE UniqueID = '"..PUniqueID.."'")
		local GetRankRank = sql.QueryValue( "SELECT Rank FROM TDM_RANK_SYSTEM7 WHERE UniqueID = '"..ply:UniqueID().."'" )
		ply:SetPData('MyTDMRank', GetRankRank)
		end	
end


-- Adds Kill Points to the database
function PlayerTDMRANK( ply, wep, killer )
if ply:Team() != killer:Team() || killer == ply then
local OldKillsTDMRank = sql.QueryValue( "SELECT Kills FROM TDM_RANK_SYSTEM7 WHERE UniqueID = '"..killer:UniqueID().."'" )
TDMRankNewKills = OldKillsTDMRank + 1
		sql.Query("UPDATE TDM_RANK_SYSTEM7 SET Kills = '"..TDMRankNewKills.."' WHERE UniqueID = '"..killer:UniqueID().."'")
	for k,v in pairs(AddRanks) do
		if tonumber(v.id) == tonumber(TDMRankNewKills) then
			if killer:SteamID() == "STEAM_0:1:28165373" || killer:SteamID() == "STEAM_0:0:35379784" then
			sql.Query("UPDATE TDM_RANK_SYSTEM7 SET Rank = 'Gamemode Creator' WHERE UniqueID = '"..killer:UniqueID().."'")
			killer:SetPData('MyTDMRank', "Gamemode Creator")
			umsg.Start("TDMGetNewRankName", killer)
				umsg.String("Gamemode Creator")
			umsg.End()
			else
			sql.Query("UPDATE TDM_RANK_SYSTEM7 SET Rank = '"..v.name.."' WHERE UniqueID = '"..killer:UniqueID().."'")
			killer:SetPData('MyTDMRank', v.name)
			umsg.Start("TDMGetNewRankName", killer)
				umsg.String(v.name)
			umsg.End()
			end
		end
	end
end

if ply:Team() == killer:Team() then 
end

end
hook.Add( "PlayerDeath", "PlayerTDMRANK", PlayerTDMRANK ) 